/*
 * Copyright (c) 2024 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */
#include <cmath>
#include "tiling_91093.h"
#include "tiling_func.h"

namespace Lcal {
    constexpr int32_t ALLREDUCE_91093_EIGHT_RANK_FP16_UBMOVENUM_DEFAULT = 160;
    constexpr int32_t ALLREDUCE_91093_EIGHT_RANK_FP16_M0_DEFAULT = 128;
    constexpr int32_t ALLREDUCE_91093_EIGHT_RANK_FP16_PVALUE_DEFAULT = 14;
    constexpr int32_t ALLREDUCE_91093_EIGHT_RANK_FP16_COMMDATASPLIT_DEFAULT = 16;
    constexpr int32_t ALLREDUCE_91093_SIXTEEN_RANK_FP16_PVALUE_DEFAULT = 14;
    constexpr int32_t ALLREDUCE_91093_SIXTEEN_RANK_FP16_UBMOVENUM_DEFAULT = 160;
    constexpr int32_t ALLREDUCE_91093_SIXTEEN_RANK_FP16_M0_DEFAULT = 128;
    constexpr int32_t ALLREDUCE_91093_SIXTEEN_RANK_FP16_COMMDATASPLIT_DEFAULT = 16;

    static std::map<int, std::vector<std::vector<int>>> g_allreduce91093EightRankFP16CommdatasplitMap = {
        {1,
         {{-1, 3072, -1, 2147483647, -1, 768}, {-1, 768, -1, 2147483647, 768, 1536},
          {768, 1536, -1, 7170, 768, 1536}, {1536, 3072, 3072, 6144, 768, 1536},
          {3072, 5148, 3072, 5634, -1, 768}, {-1, 1280, 384, 768, 1536, 3072},
          {-1, 2304, 768, 4000, 1536, 3072}, {-1, 768, 4000, 5634, 1536, 5376},
          {-1, 768, 5634, 2147483647, 1536, 3072}, {768, 1536, 4000, 5634, 1536, 3072}}},
        {16,
         {{768, 1536, 7170, 2147483647, 768, 1536}, {1536, 3072, -1, 3072, 768, 1536},
          {1536, 3072, 6144, 2147483647, 768, 1536}, {3072, 5148, -1, 3072, -1, 768},
          {3072, 5148, -1, 5634, 768, 1536}, {3072, 5148, 5634, 2147483647, -1, 1536},
          {5148, 2147483647, -1, 2147483647, -1, 1536}, {-1, 2147483647, -1, 384, 1536, 3072},
          {1280, 2147483647, 384, 768, 1536, 3072}, {2304, 2147483647, 768, 4000, 1536, 3072},
          {-1, 2147483647, -1, 4000, 3072, 2147483647}, {-1, 768, 4000, 5634, 5376, 2147483647},
          {-1, 768, 5634, 2147483647, 3072, 2147483647}, {1536, 2147483647, 4000, 5634, 1536, 3072},
          {768, 2147483647, 5634, 2147483647, 1536, 3072}, {768, 2147483647, 4000, 2147483647, 3072, 2147483647}}}
    };

    static std::map<int, std::vector<std::vector<int>>> g_allreduce91093EightRankFP16PvalueMap = {
        {4,
         {{-1, 3072, -1, 3072, -1, 768}, {5148, 31220, 3072, 4608, -1, 768},
          {-1, 3072, 768, 4608, 768, 1536}, {31220, 43740, 3072, 2147483647, -1, 1536},
          {43740, 53340, -1, 2147483647, -1, 1536}, {62400, 68160, -1, 2147483647, -1, 768},
          {68160, 2147483647, 3072, 7170, -1, 768}, {68160, 2147483647, 4608, 2147483647, 768, 1536},
          {3072, 19680, 1280, 7170, 1536, 7424}, {7350, 2147483647, 7170, 11264, 1536, 7424},
          {-1, 11904, 2976, 2147483647, 7424, 19456}, {11904, 2147483647, 7170, 2147483647, 7424, 2147483647}}},
        {1,
         {{-1, 5148, 3072, 2147483647, -1, 768}, {-1, 3072, 4608, 2147483647, 768, 1536},
          {-1, 2147483647, 11264, 2147483647, 1536, 7424}}},
        {6,
         {{3072, 31220, -1, 3072, -1, 768}, {3072, 31220, 768, 4608, 768, 1536},
          {68160, 2147483647, 3072, 4608, 768, 1536}, {19680, 2147483647, 5634, 7170, 1536, 7424}}},
        {2,
         {{5148, 31220, 4608, 2147483647, -1, 768}, {3072, 31220, 4608, 2147483647, 768, 1536},
          {68160, 2147483647, 7170, 2147483647, -1, 768}, {-1, 3072, 1280, 7170, 1536, 7424},
          {-1, 7350, 7170, 11264, 1536, 7424}, {-1, 11904, 2976, 2147483647, 19456, 2147483647}}},
        {10,
         {{-1, 31220, -1, 768, 768, 1536}, {31220, 43740, -1, 3072, -1, 1536},
          {53340, 62400, -1, 2147483647, -1, 1536}, {62400, 68160, -1, 2147483647, 768, 1536},
          {112280, 2147483647, -1, 3072, -1, 768}, {68160, 2147483647, 1536, 3072, 768, 1536},
          {-1, 38592, 768, 1280, 1536, 3072}, {-1, 5148, -1, 1280, 7424, 13312},
          {38592, 68160, 768, 1280, 1536, 3072}, {19680, 2147483647, 1280, 5634, 1536, 7424},
          {-1, 14336, 1280, 1792, 7424, 2147483647}, {11904, 2147483647, 2976, 7170, 7424, 2147483647}}},
        {12,
         {{68160, 112280, -1, 3072, -1, 768}, {-1, 38592, -1, 768, 1536, 7424},
          {-1, 38592, 768, 1280, 3072, 7424}, {-1, 38592, 768, 1280, 13312, 2147483647},
          {68160, 2147483647, 768, 1280, 1536, 3072}, {14336, 2147483647, 1280, 1792, 7424, 2147483647},
          {-1, 2147483647, 1792, 2976, 7424, 2147483647}}},
        {14,
         {{68160, 2147483647, -1, 1536, 768, 1536}, {5148, 38592, -1, 1280, 7424, 13312},
          {-1, 38592, -1, 768, 13312, 2147483647}, {38592, 2147483647, -1, 768, 1536, 2147483647},
          {38592, 2147483647, 768, 1280, 3072, 2147483647}}}
    };

    static std::map<int, std::vector<std::vector<int>>> g_allreduce91093EightRankFP16M0Map = {
        {128,
         {{-1, 3072, -1, 2147483647, -1, 10240}, {-1, 3072, -1, 3072, 10240, 19456},
          {3072, 2147483647, -1, 2147483647, -1, 19456}, {1536, 2147483647, -1, 2147483647, 19456, 2147483647}}},
        {256,
         {{-1, 3072, 3072, 2147483647, 10240, 19456}, {-1, 1536, -1, 2147483647, 19456, 2147483647}}}
    };

    static std::map<int, std::vector<std::vector<int>>> g_allreduce91093EightRankFP16UbmovenumMap = {
        {80,
         {{-1, 768, -1, 7170, -1, 768}, {31220, 36980, -1, 2147483647, -1, 768},
          {-1, 10010, -1, 3072, 1536, 3072}, {-1, 768, 3072, 2147483647, 1536, 3072}}},
        {100,
         {{-1, 768, 7170, 2147483647, -1, 768}}},
        {140,
         {{768, 3072, -1, 2147483647, -1, 768}}},
        {60,
         {{3072, 23040, -1, 3072, -1, 768}, {-1, 36980, -1, 1536, 768, 1536},
          {10010, 36980, -1, 3072, 1536, 3072}, {36980, 2147483647, -1, 1536, -1, 3072},
          {-1, 2147483647, -1, 1280, 3072, 2147483647}}},
        {20,
         {{3072, 23040, 3072, 2147483647, -1, 768}, {-1, 36980, 3072, 2147483647, 768, 1536},
          {768, 36980, 3072, 2147483647, 1536, 3072}, {36980, 142040, 3072, 4608, -1, 768},
          {36980, 2147483647, 3072, 4608, 768, 3072}, {768, 2147483647, 2976, 4608, 3072, 2147483647},
          {768, 5148, 4608, 5634, 3072, 2147483647}, {5148, 10010, 4608, 5634, 3072, 9472},
          {-1, 768, 5634, 2147483647, 3072, 2147483647}}},
        {10,
         {{23040, 31220, -1, 2147483647, -1, 768}, {142040, 2147483647, 3072, 4608, -1, 768},
          {36980, 2147483647, 4608, 2147483647, -1, 3072}, {5148, 10010, 4608, 5634, 9472, 2147483647},
          {10010, 2147483647, 4608, 5634, 3072, 2147483647}, {768, 2147483647, 5634, 2147483647, 3072, 2147483647}}},
        {30,
         {{-1, 36980, 1536, 3072, 768, 1536}, {36980, 2147483647, 1536, 3072, -1, 3072},
          {-1, 2147483647, 1280, 2976, 3072, 2147483647}, {-1, 768, 2976, 4608, 3072, 2147483647}}},
        {160,
         {{-1, 768, 4608, 5634, 3072, 2147483647}}}
    };

    static std::map<int, std::vector<std::vector<int>>> g_allreduce91093SixteenRankFP16CommdatasplitMap = {
        {1,
         {{-1, 36980, -1, 2147483647, -1, 768}, {36980, 74380, -1, 7170, -1, 768},
          {74380, 82060, -1, 3072, -1, 768}, {-1, 82060, -1, 1536, 768, 1536},
          {-1, 23040, 1536, 2147483647, 768, 1536}, {23040, 82060, 5634, 2147483647, 768, 1536},
          {82060, 2147483647, -1, 1536, -1, 1536}, {82060, 112280, 1536, 3072, -1, 1536},
          {129600, 2147483647, 1536, 3072, -1, 1536}, {176600, 222720, 3072, 2147483647, 768, 1536},
          {-1, 2147483647, -1, 2976, 1536, 10240}, {-1, 107968, -1, 2976, 10240, 13312},
          {107968, 2147483647, -1, 1792, 10240, 13312}, {-1, 2147483647, -1, 1536, 13312, 2147483647},
          {-1, 75840, 1536, 2976, 13312, 2147483647}, {-1, 11904, 2976, 2147483647, 1536, 3072},
          {-1, 3072, 2976, 2147483647, 3072, 2147483647}, {3072, 11904, 2976, 2147483647, 3072, 13312},
          {11904, 2147483647, 5634, 2147483647, 5376, 7424}}},
        {16,
         {{36980, 74380, 7170, 2147483647, -1, 768}, {74380, 82060, 3072, 2147483647, -1, 768},
          {23040, 82060, 1536, 5634, 768, 1536}, {112280, 129600, 1536, 3072, -1, 1536},
          {82060, 2147483647, 3072, 2147483647, -1, 768}, {82060, 176600, 3072, 2147483647, 768, 1536},
          {222720, 2147483647, 3072, 2147483647, 768, 1536}, {107968, 2147483647, 1792, 2976, 10240, 13312},
          {75840, 2147483647, 1536, 2976, 13312, 2147483647}, {3072, 11904, 2976, 2147483647, 13312, 2147483647},
          {11904, 2147483647, 2976, 5634, 1536, 2147483647}, {11904, 2147483647, 5634, 2147483647, 1536, 5376},
          {11904, 2147483647, 5634, 2147483647, 7424, 2147483647}}}
    };

    static std::map<int, std::vector<std::vector<int>>> g_allreduce91093SixteenRankFP16M0Map = {
        {128,
         {{-1, 2147483647, -1, 2147483647, -1, 3072}, {-1, 2147483647, -1, 2976, 3072, 2147483647}}},
        {256,
         {{-1, 2147483647, 2976, 2147483647, 3072, 2147483647}}}
    };

    static std::map<int, std::vector<std::vector<int>>> g_allreduce91093SixteenRankFP16UbmovenumMap = {
        {60,
         {{-1, 768, -1, 5634, -1, 768}, {3072, 2147483647, -1, 1536, -1, 1536},
          {3072, 36980, 1536, 3072, -1, 1536}, {-1, 15412, -1, 2976, 5376, 2147483647},
          {15412, 2147483647, -1, 2976, 1536, 13312}, {15412, 2147483647, -1, 1536, 13312, 2147483647}}},
        {20,
         {{-1, 768, 5634, 2147483647, -1, 768}, {10320, 2147483647, 3072, 4608, -1, 1536},
          {3072, 2147483647, 4608, 2147483647, -1, 1536}, {-1, 15412, 3072, 2147483647, 1536, 5376},
          {-1, 15412, 2976, 2147483647, 5376, 2147483647}, {15412, 2147483647, 2976, 2147483647, 1536, 13312},
          {15412, 2147483647, 3072, 2147483647, 13312, 2147483647}}},
        {160,
         {{768, 3072, -1, 384, -1, 768}}},
        {80,
         {{768, 3072, 384, 2147483647, -1, 768}}},
        {120,
         {{-1, 1536, -1, 4608, 768, 1536}, {1536, 3072, 640, 2147483647, 768, 1536}}},
        {40,
         {{-1, 1536, 4608, 2147483647, 768, 1536}}},
        {140,
         {{1536, 3072, -1, 640, 768, 1536}}},
        {30,
         {{36980, 2147483647, 1536, 3072, -1, 1536}, {3072, 10320, 3072, 4608, -1, 1536},
          {-1, 15412, 1536, 3072, 1536, 5376}, {15412, 2147483647, 1536, 3072, 13312, 2147483647}}},
        {100,
         {{-1, 15412, -1, 1536, 1536, 5376}}}
    };

    static std::map<int, std::vector<std::vector<int>>> g_allreduce91093SixteenRankFP16PvalueMap = {
        {4,
         {{-1, 3072, -1, 4608, -1, 768}, {5148, 31220, -1, 4608, -1, 768},
          {10010, 36980, 4608, 2147483647, 768, 1536}, {36980, 53340, 1536, 2147483647, -1, 1536},
          {53340, 68160, -1, 2147483647, -1, 768}, {68160, 74380, 3586, 2147483647, -1, 768},
          {1536, 3072, 2976, 2147483647, 3072, 2147483647}, {3072, 16340, 4608, 7170, 1536, 2147483647},
          {3072, 2147483647, 7170, 2147483647, 1536, 2147483647}}},
        {1,
         {{-1, 5148, 4608, 2147483647, -1, 768}, {-1, 1536, 2976, 7170, 1536, 5376},
          {-1, 1536, 7170, 2147483647, 1536, 2147483647}}},
        {8,
         {{3072, 5148, -1, 4608, -1, 768}, {-1, 19680, 768, 3072, 768, 1536},
          {-1, 1536, -1, 384, 10240, 2147483647}, {-1, 2560, 384, 1280, 7424, 2147483647}}},
        {12,
         {{31220, 36980, -1, 4608, -1, 768}, {-1, 36980, -1, 768, 768, 1536},
          {68160, 74380, -1, 3586, -1, 768}, {68160, 74380, 1536, 2147483647, 768, 1536},
          {-1, 19680, -1, 384, 1536, 7424}, {-1, 6298, -1, 384, 7424, 10240}}},
        {2,
         {{5148, 36980, 4608, 2147483647, -1, 768}, {-1, 10010, 3072, 2147483647, 768, 1536},
          {-1, 1536, 2976, 7170, 5376, 2147483647}, {1536, 3072, 2976, 2147483647, 1536, 3072}}},
        {10,
         {{19680, 36980, 768, 3072, 768, 1536}, {142040, 2147483647, 1536, 2147483647, -1, 768},
          {74380, 189080, 3072, 2147483647, 768, 1536}, {19680, 2147483647, 2976, 4608, 1536, 5376},
          {3072, 2147483647, 2976, 4608, 5376, 2147483647}}},
        {6,
         {{10010, 36980, 3072, 4608, 768, 1536}, {74380, 142040, 1536, 2147483647, -1, 768},
          {189080, 2147483647, 5634, 2147483647, 768, 1536}, {-1, 19680, 1536, 2976, 1536, 7424},
          {3072, 19680, 2976, 4608, 1536, 5376}, {16340, 2147483647, 4608, 7170, 1536, 2147483647}}},
        {14,
         {{36980, 53340, -1, 1536, -1, 1536}, {53340, 68160, -1, 2147483647, 768, 1536},
          {68160, 74380, -1, 1536, 768, 1536}, {74380, 2147483647, -1, 1536, -1, 768},
          {74380, 189080, -1, 3072, 768, 1536}, {189080, 2147483647, -1, 5634, 768, 1536},
          {-1, 19680, 384, 1536, 1536, 7424}, {19680, 2147483647, -1, 2976, 1536, 7424},
          {6298, 2147483647, -1, 384, 7424, 10240}, {1536, 2147483647, -1, 384, 10240, 2147483647},
          {2560, 2147483647, 384, 1280, 7424, 2147483647}, {-1, 2147483647, 1280, 2976, 7424, 2147483647}}}
    };

    void AllReduceNPU91093EightRankFP16Tiling(CoCTilingData &cocTilingData)
    {
        std::map<int*, TilingValue> tilingParamMap = {
            {&cocTilingData.commDataSplit,
             {ALLREDUCE_91093_EIGHT_RANK_FP16_COMMDATASPLIT_DEFAULT,
              g_allreduce91093EightRankFP16CommdatasplitMap}},
            {&cocTilingData.pValue,
             {ALLREDUCE_91093_EIGHT_RANK_FP16_PVALUE_DEFAULT,
              g_allreduce91093EightRankFP16PvalueMap}},
            {&cocTilingData.m0,
             {ALLREDUCE_91093_EIGHT_RANK_FP16_M0_DEFAULT,
              g_allreduce91093EightRankFP16M0Map}},
            {&cocTilingData.ubMoveNum,
             {ALLREDUCE_91093_EIGHT_RANK_FP16_UBMOVENUM_DEFAULT,
              g_allreduce91093EightRankFP16UbmovenumMap}},
            {&cocTilingData.swizzlDirect, {SWIZZLE_DIRECT_ONE}},
            {&cocTilingData.swizzlCount, {DEFAULT_SWIZZLE_COUNT}},
            {&cocTilingData.commDirect, {COMM_DATA_DIRECT}}
        };
        SetTilingParam(cocTilingData, tilingParamMap);

        cocTilingData.lenPerLoop = cocTilingData.ubMoveNum;
        cocTilingData.commNpuSplit =
                cocTilingData.commDataSplit == COMMDATASPLIT_ONE ? cocTilingData.rankSize : COMMNPUSPLIT_ONE;
        SetSecondCoreSplitTling(cocTilingData);
    }

    void AllReduceNPU91093SixteenRankFP16Tiling(CoCTilingData &cocTilingData)
    {
        std::map<int*, TilingValue> tilingParamMap = {
            {&cocTilingData.commDataSplit,
             {ALLREDUCE_91093_SIXTEEN_RANK_FP16_COMMDATASPLIT_DEFAULT,
              g_allreduce91093SixteenRankFP16CommdatasplitMap}},
            {&cocTilingData.m0,
             {ALLREDUCE_91093_SIXTEEN_RANK_FP16_M0_DEFAULT,
              g_allreduce91093SixteenRankFP16M0Map}},
            {&cocTilingData.ubMoveNum,
             {ALLREDUCE_91093_SIXTEEN_RANK_FP16_UBMOVENUM_DEFAULT,
              g_allreduce91093SixteenRankFP16UbmovenumMap}},
            {&cocTilingData.pValue,
             {ALLREDUCE_91093_SIXTEEN_RANK_FP16_PVALUE_DEFAULT,
              g_allreduce91093SixteenRankFP16PvalueMap}},
            {&cocTilingData.swizzlDirect, {SWIZZLE_DIRECT_ZERO}},
            {&cocTilingData.swizzlCount, {DEFAULT_SWIZZLE_COUNT}},
            {&cocTilingData.commDirect, {COMM_DATA_DIRECT}}
        };
        SetTilingParam(cocTilingData, tilingParamMap);

        cocTilingData.lenPerLoop = cocTilingData.ubMoveNum;
        cocTilingData.commNpuSplit =
                cocTilingData.commDataSplit == COMMDATASPLIT_ONE ? cocTilingData.rankSize : COMMNPUSPLIT_ONE;
        SetSecondCoreSplitTling(cocTilingData);
    }
}